package com.yzx.mybatis.generator.plugin;

import java.lang.reflect.Field;
import java.util.List;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;

/**
 * 解决mapper.xml文件直接追加，不进行覆盖的问题
 * @author yzx
 *
 * 创建日期 2017年6月28日 下午2:41:06
 */
public class OverwriteXmlFilePlugin extends PluginAdapter {
	
	private Field cacheField;
	
	@Override
	public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
		Class<GeneratedXmlFile> cls = GeneratedXmlFile.class;
		try {
			if(cacheField == null) {
				cacheField = cls.getDeclaredField("isMergeable");
				cacheField.setAccessible(true);
			}
			cacheField.set(sqlMap, false);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return super.sqlMapGenerated(sqlMap, introspectedTable);
	}

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

}
